package com.ruoyi.system.service.Impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.FhSchoolDeptMapper;
import com.ruoyi.system.domain.FhSchoolDept;
import com.ruoyi.system.service.IFhSchoolDeptService;
import com.ruoyi.common.core.text.Convert;

/**
 * 部门Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-25
 */
@Service
public class FhSchoolDeptServiceImpl implements IFhSchoolDeptService
{
    @Autowired(required = false)
    private FhSchoolDeptMapper fhSchoolDeptMapper;


    /**
     * 查询部门
     * 
     * @param deptId 部门主键
     * @return 部门
     */
    @Override
    public FhSchoolDept selectFhSchoolDeptByDeptId(Long deptId)
    {
        return fhSchoolDeptMapper.selectFhSchoolDeptByDeptId(deptId);
    }

    /**
     * 对象转部门树
     *
     * @param deptList 部门列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<FhSchoolDept> deptList)
    {
        return initZtree(deptList, null);
    }

    /**
     * 对象转部门树
     *
     * @param deptList 部门列表
     * @param roleDeptList 角色已存在菜单列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<FhSchoolDept> deptList, List<String> roleDeptList)
    {

        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = StringUtils.isNotNull(roleDeptList);
        for (FhSchoolDept dept : deptList)
        {
            if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()))
            {
                Ztree ztree = new Ztree();
                ztree.setId(dept.getDeptId());
                ztree.setpId(dept.getParentId());
                ztree.setName(dept.getDeptName());
                ztree.setTitle(dept.getDeptName());
                if (isCheck)
                {
                    ztree.setChecked(roleDeptList.contains(dept.getDeptId() + dept.getDeptName()));
                }
                ztrees.add(ztree);
            }
        }
        return ztrees;
    }


    /**
     * 查询部门列表
     * 
     * @param fhSchoolDept 部门
     * @return 部门
     */
    @Override
    public List<FhSchoolDept> selectFhSchoolDeptList(FhSchoolDept fhSchoolDept)
    {
        return fhSchoolDeptMapper.selectFhSchoolDeptList(fhSchoolDept);
    }

    /**
     * 新增部门
     * 
     * @param fhSchoolDept 部门
     * @return 结果
     */
    @Override
    public int insertFhSchoolDept(FhSchoolDept fhSchoolDept)
    {
        fhSchoolDept.setCreateTime(DateUtils.getNowDate());
        return fhSchoolDeptMapper.insertFhSchoolDept(fhSchoolDept);
    }

    /**
     * 修改部门
     * 
     * @param fhSchoolDept 部门
     * @return 结果
     */
    @Override
    public int updateFhSchoolDept(FhSchoolDept fhSchoolDept)
    {
        fhSchoolDept.setUpdateTime(DateUtils.getNowDate());
        return fhSchoolDeptMapper.updateFhSchoolDept(fhSchoolDept);
    }

    /**
     * 批量删除部门
     * 
     * @param deptIds 需要删除的部门主键
     * @return 结果
     */
    @Override
    public int deleteFhSchoolDeptByDeptIds(String deptIds)
    {
        return fhSchoolDeptMapper.deleteFhSchoolDeptByDeptIds(Convert.toStrArray(deptIds));
    }

    /**
     * 删除部门信息
     * 
     * @param deptId 部门主键
     * @return 结果
     */
    @Override
    public int deleteFhSchoolDeptByDeptId(Long deptId)
    {
        return fhSchoolDeptMapper.deleteFhSchoolDeptByDeptId(deptId);
    }


    @Override
    @DataScope(deptAlias = "d")
    public List<Ztree> selectFhSchoolDeptTree(FhSchoolDept fhSchoolDept)
    {
        List<FhSchoolDept> deptList = fhSchoolDeptMapper.selectFhSchoolDeptList(fhSchoolDept);
        List<Ztree> ztrees = initZtree(deptList);
        return ztrees;
    }

}
